/*
    This file is part of Desu: MapleStory v62 Server Emulator
    Copyright (C) 2014  Brent Rinchiuso <brentrin@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package data;

import field.Field;
import field.Monster;
import java.util.HashMap;
import service.Configuration;
import util.StringUtil;
import wz.WzImage;
import wz.WzObject;

/**
 * 
 * @author Brent Rinchiuso
 */
public class InformationDatabase {

    private final static HashMap<Integer, SkillInformation> SKILLS;
    private final static HashMap<Integer, SkillInformation> MONSTER_SKILLS;
    private final static HashMap<Integer, Monster> MONSTERS;

    static {
        SKILLS = new HashMap<>();
        MONSTER_SKILLS = new HashMap<>();
        MONSTERS = new HashMap<>();
    }
    
    public static SkillInformation getSkill(int id) {
        synchronized (SKILLS) {
            if (SKILLS.containsKey(id)) {
                return SKILLS.get(id);
            }
            int pre = id / 10000;
            StringBuilder path = new StringBuilder(pre == 0 ? "000" : String.valueOf(pre));
            path.append(".img");
            WzImage data = WzDataProvider.loadImage(Configuration.WZ_DIRECTORY, "Skill", path.toString());
            if (data != null) {
                String str_id = String.valueOf(id);
                if (pre == 0) {
                    str_id = "000" + str_id;
                }
                WzObject skill = data.getChild("skill");
                SkillInformation ret = new SkillInformation(id, skill.getChild(String.valueOf(str_id)));
                SKILLS.put(id, ret);
                return ret;
            }
        }
        return null;
    }
    
    public static SkillInformation getMonsterSkill(int id) {
        synchronized (MONSTER_SKILLS) {
            if (MONSTER_SKILLS.containsKey(id)) {
                return MONSTER_SKILLS.get(id);
            }
            WzImage data = WzDataProvider.loadImage(Configuration.WZ_DIRECTORY, "Skill", "MobSkill.img");
            if (data != null) {
                WzObject skill = data.getChild(String.valueOf(id));
                SkillInformation ret = new SkillInformation(id, skill);
                MONSTER_SKILLS.put(id, ret);
                return ret;
            }
        }
        return null;
    }

    public static Monster getMonster(int id, Field src) {
        synchronized (MONSTERS) {
            if (MONSTERS.containsKey(id)) {
                return new Monster(MONSTERS.get(id), src);
            }
            StringBuilder path = new StringBuilder(StringUtil.getLeftPaddedString(String.valueOf(id), '0', 7));
            path.append(".img");
            WzObject data = WzDataProvider.loadImage(Configuration.WZ_DIRECTORY, "Mob", path.toString());
            if (data != null) {
                Monster mob = new Monster(id, data);
                MONSTERS.put(id, mob);
                return new Monster(mob, src);
            }
        }
        return null;
    }
}
